package JavaSEAdvance.day07_Exception.D703_HomeWork;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.function.Predicate;

/**
 * @author Xiao
 * @date 2021/7/24
 */
public class Test02 {
    /**
     * 2.分析以下需求，并用代码实现：
     * 已知集合的元素为    aaa bbb ccc aaa bbb bbb
     * 通过今天所学内容去除集合的重复元素,分别使用迭代器和增强for循环遍历集合
     * tips:去除之后集合的元素应该为   aaa bbb ccc
     */
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        list.add("aaa");
        list.add("bbb");
        list.add("bbb");
//        Collections.addAll(list, "aaa","bbb","ccc");
        System.out.println(list);
        System.out.println("--------");
        ArrayList list1 = method2(list);
//        method1(list);
        System.out.println(list1);

        for (String s : list) {
            System.out.println(s);
        }
        System.out.println("--------");
        Iterator<String> i = list.iterator();
        while (i.hasNext()) {
            System.out.println(i.next());
        }
    }

    private static ArrayList method2(ArrayList<String> list) {
        ArrayList<String> list1 = new ArrayList<>();
        for (String s : list) {
            if (!list1.contains(s)) {
                list1.add(s);
            }
        }
        return list1;
    }

    private static void method1(ArrayList<String> list) {
        for (int i = 0; i < list.size(); i++) {
//            for (int j = 0; j < list.size(); j++) {
//                if (list.get(i).equals(list.get(j)) && i != j) {
            for (int j = i + 1; j < list.size(); j++) {
                if (list.get(i).equals(list.get(j))) {
                    list.remove(j);
                    j--;
                }

            }
        }
    }
}

/**
 * 邓超的方法：
 */
class Demo3 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("aaa");
        list.add("bbb");
        list.add("ccc");
        list.add("aaa");
        list.add("bbb");
        list.add("bbb");
        list.add("bbb");
        list.add("ddd");
        for (int i = 0; i < list.size(); i++) {
            method(list, i);
        }
        for (String s : list) {
            System.out.println(s);
        }
    }

    public static ArrayList<String> method(ArrayList<String> list, int index) {
        for (int i = index + 1; i < list.size(); i++) {
            if (list.get(i).equals(list.get(index))) {
                list.remove(i);
                i--;
            }
        }
        return list;
    }
}

/**
 * 另一个同学的方法
 */
@SuppressWarnings("all")
class Demo04 {
    public static void main(String[] args) {
        ArrayList<String> collection = new ArrayList<>();
        collection.add("aaa");
        collection.add("aaa");
        collection.add("bbb");
        collection.add("bbb");
        collection.add("ccc");
        collection.add("ccc");
        ListIterator<String> iterator = collection.listIterator();
        int count = 0;
        while (iterator.hasNext()) {    //cursor: 0 → 1 → 2 → … → size-1
            String str = iterator.next();   //str:aaa   size:6   cursor:0
            while (iterator.hasNext()) {
                String str1 = iterator.next();  //str1:aaa  size:6  cursor:1
                if (str.equals(str1)) {     //true
                    iterator.remove();      //size:5   cursor:1
                }
            }       //size:5  cursor:4
            count++;    //size:5  cursor:4  count:1
            for (int i = 0; i < collection.size() - count; i++) {   //i:0 → 3
                iterator.previous();    //cursor: 4 → 1
            }
        }
        System.out.println(collection);
    }

}

